﻿layui.use(['form', 'layer'], function () {
    var form = layui.form,
        layer = parent.layer === undefined ? layui.layer : top.layer,
        $ = layui.jquery;

    function GetCurrentUserInfo() {
       
        $.ajax({
            type: 'GET',
            url: '/InOutCash/GetOutCoinUserData/',
            data: {},
            dataType: "json",
            success: function (res) {// res为相应体,function为回调函数
                //console.log(res);
                if (res.Success) {
                    var data = res.Data;
                    $("#Id").val(data.ManagerId);
                    $("#RmmfUserId").val(data.RmmfUserId);
                    $("#txtTotalMoney").val(data.TotalMoney);
                    $("#txtBankCard").val(data.BankCard);
                    $("#txtExchangeRate").text(data.ExchangeRate);
                }
            },
            error: function (XMLHttpRequest, textStatus, errorThrown) {
                layer.alert('操作失败！！！' + XMLHttpRequest.status + "|" + XMLHttpRequest.readyState + "|" + textStatus, { icon: 5 });
            }
        });
    }

    GetCurrentUserInfo();

    $("#Money").keyup(function () {
        var rate = parseFloat($("#txtExchangeRate").text());
        var money = parseFloat($(this).val());
        var total = 0;
        if (!isNaN(money)) {
            total = Math.round(rate * money * 100) / 100;
        } 
        $("#txtRMBMoney").val(total);
    });


    form.on("submit(outCoin)", function (data) {
        //防抖
        $("#btnOutCoin").prop("disabled", true);
     
        //获取防伪标记
        $.ajax({
            type: 'POST',
            url: '/InOutCash/AddOrModifyOutCoin/',
            data: {
                ManagerId: $("#Id").val(),  //主键
                RmmfUserId: $("#RmmfUserId").val(),  //主键
                Money: $(".Money").val()
            },
            dataType: "json",
            headers: {
                "X-CSRF-TOKEN-yilezhu": $("input[name='AntiforgeryKey_yilezhu']").val()
            },
            success: function (res) {//res为相应体,function为回调函数
                if (res.ResultCode == 200) {
                    var alertIndex = layer.alert(res.ResultMsg, { icon: 1 }, function () {
                        layer.closeAll("iframe");
                        //刷新父页面
                        parent.location.reload();
                        top.layer.close(alertIndex);
                    });
                    //$("#res").click();//调用重置按钮将表单数据清空
                } else {
                    layer.alert(res.ResultMsg, { icon: 5 });
                }
            },
            error: function (XMLHttpRequest, textStatus, errorThrown) {
                layer.alert('操作失败！！！' + XMLHttpRequest.status + "|" + XMLHttpRequest.readyState + "|" + textStatus, { icon: 5 });
            }
        });
        return false;
    });

    form.verify({
        money: function (value, item) { //value：表单的值、item：表单的DOM对象
            var total = parseFloat($("#txtTotalMoney").val());
            var val = parseFloat(value);
            
            if (!isNaN(val) && !isNaN(total)) {
                if (val > total) {
                    return "可用余额不足";
                }
                if (val <= 0 || val > 6000) {
                   return "单笔最高出金6000美元";
                }
            }
        }
    });   

});

